# Replication file for the wave 2 (replication study) in Nasr (2022)
# Date: 7-Jan-2022
# Data used: data_repl.csv

# Main variables:
#             `self_placement_exp`: respondent self-placement on the experimental question (1-5 scale)
#             `exp2_condiiton`: experimental condition
#             `perceived_competence_A`: competence rating of party A
#             `perceived_competence_B`: competence rating of party B
#             `vote_A`: binary variable (1 if respondent selected party A, 0 otherwise)
#             `vote_B`: binary variable (1 if respondent selected party B, 0 otherwise)
#             `clear_party_position`: the position of party A (randomized; 1= euroskeptic; 2=europhile)

rm(list=ls())
dev.off()
# Set working directory first
data <- read.csv("data_repl.csv", as.is = T)

colnames(data)


library(tidyr) 
library(estimatr)
library(dplyr)
library(ggplot2)
library(broom)
library(gridExtra)

# Create DVs 
data$vote_diff = data$vote_B - data$vote_A
data$comp_diff = data$perceived_competence_B - data$perceived_competence_A

# barplot
# note that self-placement question was asked on a 1-5 scale in the replication study 
data %>% ggplot(aes(x=self_placement_exp)) +
  geom_bar(fill = "darkgrey") + 
  scale_x_continuous(breaks = c(seq(1,6,1)), 
                     labels = c("Strongly\nSupport", "Support", "Neither", 
                                "Oppose", "Strongly\noppose", "DK")) + 
  labs(y = "No. of respondents", x = "Self-placement", fill = "") + 
  scale_y_continuous(breaks = c(seq(0,5000, 1000)))+
  ggthemes::theme_base()

ggsave("selfplace_r.pdf", plot = last_plot(), device = "pdf", width = 7.5, height = 5) # save



# ----------------------------------------------------

# First, the average effect of ambiguity

# (1) vote choice

summary_df <-
  data %>%
  group_by(exp2_condition) %>%
  do(tidy(lm_robust(vote_diff ~ 1, data = .))) %>%
  mutate(clarity = estimate, 
         group = factor(exp2_condition, levels = c("vague", "ambivalent", "negative", "switcher")), 
         Party = "Precise")


summary_df$group<- case_when(summary_df$group == "vague" ~ "Vague", 
                             summary_df$group == "ambivalent"~ "Ambivalent", 
                             summary_df$group == "negative" ~ "Negative", 
                             summary_df$group == "switcher" ~ "Switching")
summary_df$group <- factor(summary_df$group, levels = c("Vague", "Ambivalent", "Switching", "Negative"))



a= summary_df %>% ggplot(aes(x=group, y=estimate)) + 
  geom_point() + 
  geom_pointrange(aes(ymin=conf.low, ymax=conf.high)) + 
  geom_line(group = 1) + 
  ggthemes::theme_base() +  
  geom_hline(yintercept = 0, lty=2)+
  labs(y= "Net advantage of ambiguous party in %", x="") + 
  geom_text(mapping = aes(x=1.15, y=0.13, label = "0.13"), size = 5) +
  geom_text(mapping = aes(x=2.15, y=0.045, label = "0.04"), size = 5) + 
  geom_text(mapping = aes(x=3.15, y=-0.08, label = "-0.08"), size = 5) + 
  geom_text(mapping = aes(x=4.15, y=-0.088, label = "-0.11"), size = 5) + 
  theme(text = element_text(size=20),
        axis.text.x = element_text( hjust=1)) ; a




ggsave("main_r.pdf", plot = a, device = "pdf", width = 10, height = 6) # save



t.test(data$vote_diff[data$exp2_condition=="vague"], data$vote_diff[data$exp2_condition=="ambivalent"])
t.test(data$vote_diff[data$exp2_condition=="negative"], data$vote_diff[data$exp2_condition=="switcher"])

# (2) Competence rating

summary_df <-
  data %>%
  group_by(exp2_condition) %>%
  do(tidy(lm_robust(comp_diff ~ 1, data = .))) %>%
  mutate(clarity = estimate, 
         group = factor(exp2_condition, levels = c("vague", "ambivalent", "negative", "switcher")), 
         Party = "Precise")


summary_df$group<- case_when(summary_df$group == "vague" ~ "Vague", 
                             summary_df$group == "ambivalent"~ "Ambivalent", 
                             summary_df$group == "negative" ~ "Negative", 
                             summary_df$group == "switcher" ~ "Switching")
summary_df$group <- factor(summary_df$group, levels = c("Vague", "Ambivalent", "Switching", "Negative"))


b= summary_df %>% ggplot(aes(x=group, y=estimate)) + 
  geom_point() + 
  geom_pointrange(aes(ymin=conf.low, ymax=conf.high)) + 
  geom_line(group = 1) + 
  ggthemes::theme_base() +  
  geom_hline(yintercept = 0, lty=2)+
  labs(y= "Net advantage of ambiguous party", x="") + 
  geom_text(mapping = aes(x=1.15, y=0.52, label = "0.5"),  size = 5) +
  geom_text(mapping = aes(x=2.15, y=0.13, label = "0.07"), size = 5) + 
  geom_text(mapping = aes(x=3.15, y=-0.8, label = "-0.8"), size = 5) + 
  geom_text(mapping = aes(x=4.15, y=-0.6, label = "-0.6"), size = 5) + 
  theme(text = element_text(size=20),
        axis.text.x = element_text( hjust=1)); b




t.test(data$comp_diff[data$exp2_condition=="vague"], data$comp_diff[data$exp2_condition=="ambivalent"])
t.test(data$comp_diff[data$exp2_condition=="negative"], data$comp_diff[data$exp2_condition=="switcher"])


ggsave("comp_r.pdf", plot = b, device = "pdf", width = 10, height = 6)



# -----------------------------------------------------
# Second, we create the datasets for the three scenarios as in the main analysis

# Recode DKs
data$self_placement_exp[data$self_placement_exp>5] = 3 # set DKs to midpoint


# Scenario 1: the rival party agrees with the voter

sc1 = data %>% dplyr::filter(clear_party_position==1 & self_placement_exp>3 | 
                               clear_party_position==2 & self_placement_exp<3)

# Scenario 2: the rival party disagrees with the voter

sc2 = data %>% dplyr::filter(clear_party_position==2 & self_placement_exp>3 | 
                               clear_party_position==1 & self_placement_exp<3)

# Scenario 3: when the voter is indifferent or undecided


sc3 = data %>% dplyr::filter(self_placement_exp==3)

# Estimates with SEs and CIs
one <-
  sc1 %>%
  group_by(exp2_condition) %>%
  do(tidy(lm_robust(vote_diff ~ 1, data = .))) %>%
  mutate(clarity = estimate, 
         group = factor(exp2_condition, levels = c("vague", "ambivalent", "switcher", "negative")), 
         type = "Disadvantegeous")

two <-
  sc2 %>%
  group_by(exp2_condition) %>%
  do(tidy(lm_robust(vote_diff ~ 1, data = .))) %>%
  mutate(clarity = estimate, 
         group = factor(exp2_condition, levels = c("vague", "ambivalent", "switcher", "negative")), 
         type = "Advanteguous")

three <-
  sc3 %>%
  group_by(exp2_condition) %>%
  do(tidy(lm_robust(vote_diff ~ 1, data = .))) %>%
  mutate(clarity = estimate, 
         group = factor(exp2_condition, levels = c("vague", "ambivalent", "switcher", "negative")), 
         type = "Neutral")


# Labels
one$group<- case_when(one$group == "vague" ~ "Vague", 
                      one$group == "ambivalent"~ "Ambivalent", 
                      one$group == "negative" ~ "Negative", 
                      one$group == "switcher" ~ "Switching")
one$group <- factor(one$group, levels = c("Vague", "Ambivalent", "Switching","Negative"))

two$group<-  case_when(two$group == "vague" ~ "Vague", 
                       two$group == "ambivalent"~ "Ambivalent", 
                       two$group == "negative" ~ "Negative", 
                       two$group == "switcher" ~ "Switching")
two$group <- factor(two$group, levels = c("Vague", "Ambivalent", "Switching","Negative"))

three$group<- case_when(three$group == "vague" ~ "Vague", 
                        three$group == "ambivalent"~ "Ambivalent", 
                        three$group == "negative" ~ "Negative", 
                        three$group == "switcher" ~ "Switching")
three$group <- factor(three$group, levels = c("Vague", "Ambivalent", "Switching","Negative"))




c1= one %>% ggplot(aes(x=group, y=estimate)) + 
  geom_point() + 
  geom_pointrange(aes(ymin=conf.low, ymax=conf.high)) + 
  geom_line(group = 1) + 
  ggthemes::theme_base() +  
  geom_hline(yintercept = 0, lty=2)+
  labs(x= "Competence differential (> ambiguous)", title = "Scenario 1: Disadvantageous competition",
       y="", 
       color = "", tag = "A") +
  geom_text(mapping = aes(x=1.15, y=-0.28, label = "-0.3"),  size = 5) +
  geom_text(mapping = aes(x=2.15, y=-0.3, label = "-0.32"), size = 5) + 
  geom_text(mapping = aes(x=3.15, y=-0.43, label = "-0.45"), size = 5) + 
  geom_text(mapping = aes(x=4.15, y=-0.5, label = "-0.52"), size = 5) +
  theme(axis.text=element_text(size=16, face = "bold"))+
  labs(x=""); c1


c2= two %>% ggplot(aes(x=group, y=estimate)) + 
  geom_point() + 
  geom_pointrange(aes(ymin=conf.low, ymax=conf.high)) + 
  geom_line(group = 1) + 
  ggthemes::theme_base() +  
  geom_hline(yintercept = 0, lty=2)+
  labs(x= "Competence differential (> ambiguous)", title = "Scenario 2: Advantageous competition",
       y="", 
       color = "", tag = "B") +
  theme(axis.text=element_text(size=16, face = "bold"))+
  geom_text(mapping = aes(x=1.15, y=0.5, label = "0.49"),  size = 5) +
  geom_text(mapping = aes(x=2.15, y=0.35, label = "0.35"), size = 5) + 
  geom_text(mapping = aes(x=3.15, y=0.22, label = "0.2"), size = 5) + 
  geom_text(mapping = aes(x=4.17, y=0.25, label = "0.23"), size = 5) +
  labs(x=""); c2

c3= three %>% ggplot(aes(x=group, y=estimate)) + 
  geom_point() + 
  geom_pointrange(aes(ymin=conf.low, ymax=conf.high)) + 
  geom_line(group = 1) + 
  ggthemes::theme_base() +  
  geom_hline(yintercept = 0, lty=2)+
  labs(x= "Competence differential (> ambiguous)", title = "Scenario 3: Neutral competition",
       y="", 
       color = "", tag = "C") +
  theme(axis.text=element_text(size=16, face = "bold"))+
  geom_text(mapping = aes(x=1.15, y=0.17, label = "0.16"),  size = 5) +
  geom_text(mapping = aes(x=2.15, y=0.08, label = "0.07"), size = 5) + 
  geom_text(mapping = aes(x=3.15, y=-0.04, label = "-0.04"), size = 5) + 
  geom_text(mapping = aes(x=4.15, y=-0.07, label = "-0.08"), size = 5) +
  labs(x=""); c3

# Combine into one figure
layout_matrix <- matrix(c(1, 1, 2, 2, 4, 3, 3, 4), nrow = 2, byrow = TRUE)
plot= grid.arrange(c1, c2, c3, layout_matrix = layout_matrix)


ggsave("sc_r.pdf", plot = plot, device = "pdf", width = 15, height = 8.71)

